<?php

namespace App\Listener;

use App\Event\UserOnlineEvent;
use App\Helpers\Helper;
use App\Job\SendSysgiftJob;
use App\Model\Setting\SettingAllocate;
use App\Model\User;
use App\Model\UserProfile;
use Hyperf\Event\Annotation\Listener;
use Hyperf\Event\Contract\ListenerInterface;

/**
 * 发送系统礼物 - 监听器
 * @Listener 
 */
class SendSysGiftListener implements ListenerInterface
{
    //分阶段推送间隔
    public $pushOffset = 20;

    public function listen(): array
    {
        return [
            //上线事件
            // UserOnlineEvent::class,
        ];
    }

    /**
     * @param UserRegistered $event
     */
    public function process(object $event)
    {
        //礼物是否需要领取
        switch ($event->eventType) {
                //上线
            case 'online':
                if ($event->onlineStatus == 0) return false;
                $this->proccessOnline($event->userId[0]);
                break;
        }
        return;
    }

    /**
     * 上线男用户的系统礼物逻辑
     */
    function proccessOnline($userId)
    {
        //非男用户不处理
        $profile = UserProfile::getProfile($userId);
        if (User::checkOfficial($userId) || !$profile || $profile->gender != 1) return false;

        //查询被分配的组，按分配的组进行发放
        // $group = SettingAllocate::getHostGroupByUser($userId);
        //系统礼物
        // $giftIds = SettingAllocate::getGiftIds();
        $group = [
            [
                'host_cate' => UserProfile::HOST_CATE_OFFICIAL,
                'gift_id' => 25,
                'gift_num' => 50000,
                'single_gift_num' => 6,
                'gift_expire_time' => 300,
            ]
        ];
        //如果没有被分配的信息
        if (empty($group)) {
            log_user_result($userId, "NoAllocateGroup");
            return false;
        }
        foreach ($group as $batch => $groupItem) {
            log_user_result($userId, "GroupCate:{$groupItem['host_cate']}");
            Helper::pushRedisAsync(new SendSysgiftJob([
                'user_id' => $userId,
                'group' => $groupItem
            ]), $batch * $this->pushOffset, 'push_msg');
        }
        return;
    }
}
